package org.example.java_spring_boot.mapper;

import org.apache.ibatis.annotations.*;
import org.example.java_spring_boot.entiry.Student;

import java.util.List;

@Mapper
public interface StudentMapper {
    @Results(id = "studentMap", value ={
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "number", column = "no"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "qq", column = "qq")
    })

    @Select("select * from tb_student")
    List<Student> getAll();

    @ResultMap("studentMap")
    @Select("select * from tb_student where no = #{no}")
    List<Student> getStudentByNumber(String no);

    @Insert("insert into tb_student(no,name,sex,phone,qq) values(#{number},#{name},#{sex},#{phone},#{qq})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", resultType = Integer.class, before = false)
    int addStudent(Student student);

    @Update("update tb_student set name=#{name}, sex=#{sex}, phone=#{phone}, qq=#{qq} where no=#{number}")
    int updateStudent(Student student);

    @Delete("delete from tb_student where no=#{no}")
    int deleteStudentByNumber(String no);
}